package demo1;

import java.util.LinkedList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Queue;

public class BinaryTree {
    //创建二叉树
    public TreeNode creatTree() {
        TreeNode root = new TreeNode(3);
        TreeNode B = new TreeNode(5);
        TreeNode C = new TreeNode(1);
        TreeNode D = new TreeNode(6);
        TreeNode E = new TreeNode(2);
        TreeNode F = new TreeNode(8);
        TreeNode G = new TreeNode(7);
        TreeNode H = new TreeNode(4);

        root.left = B;
        root.right = C;
        B.left = D;
        B.right = E;
        C.left = F;

        return root;
    }

    //计算结点个数
    //左子树节点数加右子树节点数加根节点
    public int size(TreeNode root) {
        if(root == null) {
            return 0;
        }
        return size(root.left) + size(root.right) + 1;
    }

    public static void main(String[] args) {
        BinaryTree tree = new BinaryTree();
        TreeNode root = tree.creatTree();
        /*System.out.println(tree.size(root));
        System.out.println(tree.getLeafNodes(root));
        System.out.println(tree.getHeight(root));*/

//        System.out.println(tree.getKNodes(root,3));
/*        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(null);
        queue.offer(null);
        System.out.println(queue.size());*/
        //System.out.println(tree.isCompleteTree(root));
        StringBuilder stringBUilder = new StringBuilder();

    }

    public int getLeafNodes(TreeNode root) {
        if(root == null) {
            return 0;
        }
        if(root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNodes(root.left) + getLeafNodes(root.right);
    }

    public int getHeight(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);

        return Math.max(leftHeight,rightHeight) + 1;
    }

    public int getKNodes(TreeNode root,int k) {
        if(root == null) {
            return 0;
        }
        if(k == 1) {
            return 1;
        }
        return getKNodes(root.left,k-1) + getKNodes(root.right,k-1);
    }

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null) {
            return null;
        }

        if(root == p || root == q) {
            return root;
        }

        TreeNode left = lowestCommonAncestor(root.left,p,q);

        TreeNode right = lowestCommonAncestor(root.right,p,q);

        if(left != null && right == null) {
            return left;
        }

        if(left == null && right != null) {
            return right;
        }

        return root;
    }

    public boolean isCompleteTree(TreeNode root) {
        if(root == null) {
            return true;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while(!queue.isEmpty()) {
            TreeNode tmp = queue.poll();
            if(tmp != null) {
                queue.offer(tmp.left);
                queue.offer(tmp.right);
            } else {
                break;
            }
        }

        while(!queue.isEmpty()) {
            TreeNode tmp = queue.poll();
            if(tmp != null) {
                return false;
            }
        }

        return true;
    }

    int[] elem;

    public void siftUp(int child) {
        int parent = (child - 1) / 2;
        while(parent >= 0) {
            if(elem[parent] < elem[child]) {
                int tmp = elem[parent];
                elem[parent] = elem[child];
                elem[child] = tmp;
                child = parent;
                parent = (child - 1) / 2;
            } else {
                break;
            }
        }
    }


}
